//
//  HMMainViewController.swift
//  WeiboZH
//
//  Created by acer on 16/10/4.
//  Copyright © 2016年 acer. All rights reserved.
//

import UIKit

class HMMainViewController: UITabBarController {

    override func viewDidLoad() {
        super.viewDidLoad()
        let tab = HMTabBar()
        //设置撰写按钮点击的事件响应
        // 注意：此闭包内使用 `self` 会形成循环引用
        tab.composeButtonClosure = {
            print("撰写按钮点击")
        }
        // 设置背景图片，去掉顶部阴影的效果
        tab.backgroundImage = UIImage(named: "tabbar_background")
        setValue(tab, forKey: "tabBar")
        //  设置tintColor -> 统一设置tabbar的选中颜色
        //  越早执行越好,一般会Appdelegate里面
        UITabBar.appearance().tintColor = UIColor.orange
        addChildViewControllers()
    }
    
    /// 添加控制器
    ///
    /// - parameter vc       : 视图控制器
    /// - parameter title    : 标题
    /// - parameter imageName: 图像名称
    private func addChildViewController(vc: UIViewController, title: String, imageName: String) {
        //设置标题
        vc.title = title
        //设置图片
        vc.tabBarItem.image = UIImage(named: imageName)
        vc.tabBarItem.selectedImage = UIImage(named: "\(imageName)_selected")
        vc.tabBarItem.setTitleTextAttributes([NSForegroundColorAttributeName: UIColor.orange], for: .selected)
        //  设置文字大小
        vc.tabBarItem.setTitleTextAttributes([NSFontAttributeName: UIFont.systemFont(ofSize: 14)], for: .normal)
        //使用导航控制器包裹起来
        let nav = HMNavigationController(rootViewController: vc)
        addChildViewController(nav)
    }
   
    /// 添加所有子控制器
    private func addChildViewControllers() {
        addChildViewController(vc: HMHomeTableViewController(), title: "首页", imageName: "tabbar_home")
        addChildViewController(vc: HMMessageTableViewController(), title: "消息", imageName: "tabbar_message_center")
        addChildViewController(vc: HMDiscoverTableViewController(), title: "发现", imageName: "tabbar_discover")
        addChildViewController(vc: HMProfileTableViewController(), title: "我", imageName: "tabbar_profile")
    }


    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    



}

